package top.shenluw.springdemo.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.LockModeType;

/**
 * @author Shenluw
 * created：2019/7/22 11:57
 */
public interface UserRepository extends JpaRepository<User, Integer> {

	User findByName(String name);

	@Lock(LockModeType.PESSIMISTIC_WRITE)
	@Query("select u from User u where u.id = ?1")
	User findByIdForWriteLock(int id);

	@Lock(LockModeType.PESSIMISTIC_READ)
	@Query("select u from User u where u.id = ?1")
	User findByIdForReadLock(int id);

	// PESSIMISTIC_READ
	// select * where user0_.id=? lock in share mode
	// PESSIMISTIC_WRITE
	// select * from user user0_ where user0_.id=? for update
	// PESSIMISTIC_FORCE_INCREMENT
	// 读取也会增加版本号
	// select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_, user0_.version as version4_0_ from user user0_ where user0_.id=? for update
	// OPTIMISTIC_FORCE_INCREMENT
	// 读取也会增加版本号
}
